import Vue from 'vue';
import Vuex from 'vuex';
import router from '../router'
Vue.use(Vuex)
export default new Vuex.Store({
  
    state: {
        shopping: [{
            canUseAccount: "",
            description: "木质夹子，多用途固定夹[1个]",
            expectConsumeDay: 16,
            flagShunfeng: "0",
            gmtCreate: "2018-08-14 19:02:04",
            gmtModify: "2019-12-25 19:10:29",
            id: 9,
            isPublic: "1",
            lineThreshold: 15,
            matchingCode: "JZ001",
            picList: '["http://baoyitech.oss-cn-hangzhou.aliyuncs.com/1556101568000b008233379420737cddf9f69aef2fbf6"]',
            picsListAll: '["http://baoyitech.oss-cn-hangzhou.aliyuncs.com/1556101568000b008233379420737cddf9f69aef2fbf6","http://baoyitech.oss-cn-hangzhou.aliyuncs.com/1568965907890266694f8c6f6a9aa9612cdc24a91f488","http://baoyitech.oss-cn-hangzhou.aliyuncs.com/1568965912377afc7b4188d014e56fe8c202e5a67ff72"]',
            presaleBuyNum: 647,
            presaleInStorageNum: 647,
            presaleLockStockNum: 0,
            presaleNotInStorageNum: 0,
            price: 0.1,
            productId: "GN201808146715279612",
            productProhibitedState: "1",
            productStatus: "1",
            purchaseCycleTime: "6",
            salesNum: 312011,
            sevenAvgNum: 1116,
            sortIndex: 1200,
            statusDetail: "上架",
            stockNum: 16942,
            taobaoPrice: 0.2,
            title: "晾衣夹",
            weight: 0.01,
            num: 1,
            n:1,
            isChecked: true,
        }],
        loginState: false, //模拟登录状态
        username: '',
        addressInfo: [{
                username: '小猪佩奇',
                phone: '13988886666',
                tel: ['023', '58762390'],
                loc: [{
                        name: '北京',
                        idx: 0
                    },
                    {
                        name: '朝阳',
                        idx: 2
                    },
                    {
                        name: '',
                        idx: -1
                    },
                ],
                address: '3号巷子6号胡同',
                isDefault: true
            },
            
        ]
    },
    getters: {
        countnum(state) {
            var allnum = 0,
                allprice = 0,
                price = 0,
                allCheck = true,
                checkNum=0,
                checkPrice=0,
                checkDate=[]
            state.shopping.forEach((val) => {
                allnum += val.num;
                price = val.price * val.num
                allprice = price.toFixed(2)
                if (!val.isChecked) {
                    allCheck = false
                }
                if(val.isChecked){
                    checkNum+=val.num;
                    checkPrice+=val.num*val.price,
                    checkDate.push(val)
                }
            })
            return {
                allnum,
                allprice,
                allCheck,
                checkPrice,
                checkNum,
                checkDate
            }
        }
    },
    mutations: {
        increasedata(state, obj) {
            var bool = true;
            state.shopping.forEach((val) => {
                if (val.id == obj.id) {
                    val.num++;
                    bool = false;
                    return false
                }
            })
            if (bool) {
                Vue.set(obj, "num", 1);
                Vue.set(obj, "isChecked", true)
                state.shopping.push(obj)
            }
        },
        loginMut(state, obj) { //登录状态记录数据
            if (obj.bool != false) {
                state.loginState = true;
                state.username = obj.username

            } else {
                state.loginState = false;
                state.username = ''
            }

        },
        addAddressMut(state, obj) { //新增或保存
            if (obj.idx == -1) {
                state.addressInfo.push(obj.newObj)
            } else {
                state.addressInfo[obj.idx] = obj.newObj
            }

        },
        addressDel(state, i) { //删除地址的某一条
            state.addressInfo.splice(i, 1)
        },
        cartCheckedMut(state, i) { //购物车单选按钮
            state.shopping[i].isChecked = !state.shopping[i].isChecked
        },
        cartAllCheckMut(state, bool) { //全选按钮
            state.shopping.forEach((item) => {
                item.isChecked = !bool
            })
        },
        cartDel(state,i){
            state.shopping.splice(i,1)
        }
    },
    actions: {
        loginAct(context, obj) {
            Vue.prototype.$http.post('/users/login', obj).then((res) => {
                console.log(res.data.username)
                if (res.data.code == 1) {
                    context.commit('loginMut', {
                        bool: true,
                        username: res.data.username
                    })
                    router.push({
                        path: '/cart'
                    })
                    localStorage.setItem('userName', res.data.username)
                    return false
                }
                Vue.prototype.$message({ //如果用户名密码错误，则进行提示
                    message: res.data.msg,
                    type: 'warning'
                })
            })
        },
        cartDel(state,i){
            state.shopping.splice(i,1)
        }
    }

})